<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="marc">
  <xsl:import href="meercat.mapreduce.mappers:xslt/MARC21slimUtils.xsl" />
  <xsl:output method="xml" indent="yes"/>
  <!--
  2009/03/27 - Started from http://www.loc.gov/standards/marcxml/xslt/MARC21slim2OAIDC.xsl
  -->

  <xsl:template match="marc:record">
    <doc>
      <xsl:variable name="leader6" select="substring(marc:leader,7,1)"/>
      <xsl:variable name="isHolding"
        select="$leader6='u' or $leader6='v' or $leader6='x' or $leader6='y'"/>

      <xsl:apply-templates select="./*">
        <xsl:with-param name="isHolding" select="$isHolding"/>
      </xsl:apply-templates>
      <!--
      <xsl:apply-templates select="marc:datafield">
        <xsl:with-param name="isHolding" select="$isHolding"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="marc:datafield/marc:subfield">
        <xsl:with-param name="isHolding" select="$isHolding"/>
      </xsl:apply-templates>
      -->
      
      <!-- If this is a bibliographic record and there is no 007 field, assume medium print -->
      <xsl:if test="not($isHolding) and not(marc:controlfield[@tag=007])">
      	<field name="medium">Print</field>
      </xsl:if>
    </doc>
  </xsl:template>

  <!-- Suppress all unmatched text/attributes -->
  <xsl:template match="text()|@*"/>

  <xsl:template match="marc:leader">
    <xsl:param name="isHolding"/>
    <xsl:variable name="leader" select="."/>
    <xsl:variable name="leader6" select="substring($leader,7,1)"/>
    <xsl:variable name="leader7" select="substring($leader,8,1)"/>

    <xsl:if test="not($isHolding)">
      <xsl:if test="$leader7='c'">
        <field name="type">Collection</field>
      </xsl:if>
      <xsl:choose>
        <xsl:when
          test="$leader6='a' or $leader6='c' or $leader6='d' or $leader6='e' or $leader6='f' or $leader6='t'">
          <field name="type">Text</field>
        </xsl:when>
        <xsl:when test="$leader6='i' or $leader6='j'">
          <field name="type">Sound</field>
        </xsl:when>
        <xsl:when test="$leader6='k'">
          <field name="type">Image</field>
          <field name="format">Image</field>
        </xsl:when>
        <xsl:when test="$leader6='g'">
          <field name="type">Moving Image</field>
          <field name="format">Video</field>
        </xsl:when>
        <xsl:when test="$leader6='r' or $leader6='o'">
          <field name="type">Physical Object</field>
        </xsl:when>
        <xsl:when test="$leader6='m'">
          <field name="type">Software</field>
        </xsl:when>
        <xsl:when test="$leader6='p'">
          <field name="type">Mixed Material</field>
        </xsl:when>
      </xsl:choose>

      <xsl:choose>
        <xsl:when test="$leader6='a'">
          <xsl:choose>
            <xsl:when test="$leader7='s'">
              <field name="format">Journal</field>
            </xsl:when>
            <xsl:otherwise>
              <field name="format">Book</field>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="$leader6='i'">
          <field name="format">Spoken Word</field>
        </xsl:when>
        <xsl:when test="$leader6='c' or $leader6='d' or $leader6='j'">
          <field name="format">Music</field>
        </xsl:when>
        <xsl:when test="$leader6='e' or $leader6='f'">
          <field name="format">Cartographic</field>
        </xsl:when>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template match="marc:controlfield[@tag=007]">
    <xsl:param name="isHolding"/>
    <xsl:variable name="category" select="substring(.,1,1)"/>
    <xsl:variable name="designation" select="substring(.,2,1)"/>

    <xsl:choose>
      <xsl:when test="$category='a' or $category='d'">
        <xsl:variable name="medium" select="substring(.,5,1)"/>
        <xsl:choose>
          <xsl:when test="$medium='a'">
            <field name="medium">Paper</field>
          </xsl:when>
          <xsl:when test="$medium='b'">
            <field name="medium">Wood</field>
          </xsl:when>
          <xsl:when test="$medium='c'">
            <field name="medium">Stone</field>
          </xsl:when>
          <xsl:when test="$medium='d'">
            <field name="medium">Metal</field>
          </xsl:when>
          <xsl:when test="$medium='e'">
            <field name="medium">Synthetic</field>
          </xsl:when>
          <xsl:when test="$medium='f'">
            <field name="medium">Skin</field>
          </xsl:when>
          <xsl:when test="$medium='g'">
            <field name="medium">Textiles</field>
          </xsl:when>
          <xsl:when test="$medium='j'">
            <field name="medium">Glass</field>
          </xsl:when>
          <xsl:when test="$medium='p'">
            <field name="medium">Plaster</field>
          </xsl:when>
          <xsl:when test="$medium='q' or $medium='r' or $medium='s' or $medium='t' or $medium='y'">
            <field name="medium">Photograph</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Map/Globe</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='c'">
        <field name="medium">Electronic</field>
      </xsl:when>
      <xsl:when test="$category='f'">
        <field name="medium">Tactile</field>
      </xsl:when>
      <xsl:when test="$category='g'">
        <xsl:choose>
          <xsl:when test="$designation='o' or $designation='d' or $designation='f'">
            <field name="medium">Filmstrip</field>
          </xsl:when>
          <xsl:when test="$designation='s' or $designation='t'">
            <field name="medium">Transparency</field>
          </xsl:when>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='h'">
        <xsl:choose>
          <xsl:when test="$designation='a'">
            <field name="medium">Aperture Card</field>
          </xsl:when>
          <xsl:when test="$designation='b'">
            <field name="medium">Microfilm Cartridge</field>
          </xsl:when>
          <xsl:when test="$designation='c'">
            <field name="medium">Microfilm Cassette</field>
          </xsl:when>
          <xsl:when test="$designation='d'">
            <field name="medium">Microfilm Reel</field>
          </xsl:when>
          <xsl:when test="$designation='e'">
            <field name="medium">Microfiche</field>
          </xsl:when>
          <xsl:when test="$designation='f'">
            <field name="medium">Microfiche Cassette</field>
          </xsl:when>
          <xsl:when test="$designation='g'">
            <field name="medium">Microopaque</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Microform</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='k'">
        <xsl:variable name="medium" select="substring(.,5,1)"/>
        <xsl:choose>
          <xsl:when test="$medium='a'">
            <field name="medium">Canvas</field>
          </xsl:when>
          <xsl:when test="$medium='b'">
            <field name="medium">Bristol Board</field>
          </xsl:when>
          <xsl:when test="$medium='c'">
            <field name="medium">Cardboard/Illustration Board</field>
          </xsl:when>
          <xsl:when test="$medium='d'">
            <field name="medium">Glass</field>
          </xsl:when>
          <xsl:when test="$medium='e'">
            <field name="medium">Synthetic</field>
          </xsl:when>
          <xsl:when test="$medium='f'">
            <field name="medium">Skin</field>
          </xsl:when>
          <xsl:when test="$medium='g'">
            <field name="medium">Textiles</field>
          </xsl:when>
          <xsl:when test="$medium='h'">
            <field name="medium">Metal</field>
          </xsl:when>
          <xsl:when test="$medium='m'">
            <field name="medium">Mixed Collection</field>
          </xsl:when>
          <xsl:when test="$medium='o'">
            <field name="medium">Paper</field>
          </xsl:when>
          <xsl:when test="$medium='p'">
            <field name="medium">Plaster</field>
          </xsl:when>
          <xsl:when test="$medium='q'">
            <field name="medium">Hardboard</field>
          </xsl:when>
          <xsl:when test="$medium='r'">
            <field name="medium">Porcelain</field>
          </xsl:when>
          <xsl:when test="$medium='s'">
            <field name="medium">Stone</field>
          </xsl:when>
          <xsl:when test="$medium='t'">
            <field name="medium">Wood</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Graphic</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='m'">
        <field name="medium">Movie Reel</field>
      </xsl:when>
      <xsl:when test="$category='o'">
        <field name="medium">Kit</field>
      </xsl:when>
      <xsl:when test="$category='q'">
        <field name="medium">Notated Music</field>
      </xsl:when>
      <xsl:when test="$category='r'">
        <field name="medium">Remote-Sensing Image</field>
      </xsl:when>
      <xsl:when test="$category='s'">
        <xsl:variable name="dimension" select="substring(.,7,1)"/>
        <xsl:choose>
          <xsl:when test="$designation='d' and $dimension='g'">
            <field name="medium">CD</field>
          </xsl:when>
          <xsl:when test="$designation='s' and $dimension='j'">
            <field name="medium">Cassette</field>
          </xsl:when>
          <xsl:when test="$designation='d' and $dimension='e'">
            <field name="medium">LP</field>
          </xsl:when>
          <xsl:when test="$designation='d' and not($dimension='g' or $dimension='e')">
            <field name="medium">Sound Disc</field>
          </xsl:when>
          <xsl:when test="$designation='e'">
            <field name="medium">Sound Cylinder</field>
          </xsl:when>
          <xsl:when test="$designation='g'">
            <field name="medium">Sound Cartridge</field>
          </xsl:when>
          <xsl:when test="$designation='i'">
            <field name="medium">Sound-track Film</field>
          </xsl:when>
          <xsl:when test="$designation='q'">
            <field name="medium">Sound Roll</field>
          </xsl:when>
          <xsl:when test="$designation='s' and not($dimension='j')">
            <field name="medium">Sound Cassette</field>
          </xsl:when>
          <xsl:when test="$designation='t'">
            <field name="medium">Sound Reel</field>
          </xsl:when>
          <xsl:when test="$designation='w'">
            <field name="medium">Sound Wire</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Sound Recording</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='t'">
        <xsl:choose>
          <xsl:when test="$designation='b'">
            <field name="medium">Large Print</field>
          </xsl:when>
          <xsl:when test="$designation='c'">
            <field name="medium">Braille</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Print</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$category='v'">
        <xsl:variable name="medium" select="substring(.,5,1)"/>
        <xsl:choose>
          <xsl:when test="$medium='a'">
            <field name="medium">Beta</field>
          </xsl:when>
          <xsl:when test="$medium='b'">
            <field name="medium">VHS</field>
          </xsl:when>
          <xsl:when test="$medium='c'">
            <field name="medium">U-matic</field>
          </xsl:when>
          <xsl:when test="$medium='d'">
            <field name="medium">EIAJ</field>
          </xsl:when>
          <xsl:when test="$medium='e'">
            <field name="medium">Type C</field>
          </xsl:when>
          <xsl:when test="$medium='f'">
            <field name="medium">Quadruplex</field>
          </xsl:when>
          <xsl:when test="$medium='g'">
            <field name="medium">Laserdisc</field>
          </xsl:when>
          <xsl:when test="$medium='h'">
            <field name="medium">CED videodisc</field>
          </xsl:when>
          <xsl:when test="$medium='i'">
            <field name="medium">Betacam</field>
          </xsl:when>
          <xsl:when test="$medium='j'">
            <field name="medium">Betacam SP</field>
          </xsl:when>
          <xsl:when test="$medium='k'">
            <field name="medium">Super-VHS</field>
          </xsl:when>
          <xsl:when test="$medium='m'">
            <field name="medium">M-II</field>
          </xsl:when>
          <xsl:when test="$medium='o'">
            <field name="medium">D-2</field>
          </xsl:when>
          <xsl:when test="$medium='p'">
            <field name="medium">8mm</field>
          </xsl:when>
          <xsl:when test="$medium='q'">
            <field name="medium">Hi-8mm</field>
          </xsl:when>
          <xsl:when test="$medium='s'">
            <field name="medium">Blu-ray disc</field>
          </xsl:when>
          <xsl:when test="$medium='v'">
            <field name="medium">DVD</field>
          </xsl:when>
          <xsl:otherwise>
            <field name="medium">Videorecording</field>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <field name="medium">Print</field>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="marc:controlfield[@tag=008]">
    <xsl:param name="isHolding"/>

    <xsl:if test="not(isHolding)">
      <xsl:variable name="type" select="substring(.,7,1)"/>
      <xsl:variable name="date1" select="substring(.,8,4)"/>
      <xsl:variable name="date2" select="substring(.,12,4)"/>
      <xsl:if test="substring(.,36,3)">
      <field name="language">
        <xsl:value-of select="substring(.,36,3)"/>
      </field>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="$type='c' or $type='d' or $type='u'">
          <field name="datePublished"><xsl:value-of select="$date1"/>-<xsl:value-of select="$date2" /></field>
        </xsl:when>
        <xsl:when test="$type='i' or $type='k'">
          <field name="date"><xsl:value-of select="$date1"/>-<xsl:value-of select="$date2" /></field>
        </xsl:when>
        <xsl:when test="$type='e' or $type='s'">
          <field name="date"><xsl:value-of select="$date1" /></field>
        </xsl:when>
        <xsl:when test="$type='m' or $type='p' or $type='r'">
          <field name="date"><xsl:value-of select="$date1" /></field>
          <field name="date"><xsl:value-of select="$date2" /></field>
        </xsl:when>
        <xsl:when test="$type='t'">
          <field name="datePublished"><xsl:value-of select="$date1" /></field>
          <field name="dateCopyrighted"><xsl:value-of select="$date2" /></field>
        </xsl:when>
      </xsl:choose>
      
      <xsl:if test="$type='c' or $type='d'">
        <xsl:variable name="frequency" select="substring(.,19,1)"/>
        <xsl:choose>
          <xsl:when test="$frequency='a'">
            <field name="accrualPeriodicity">Annual</field>
          </xsl:when>
          <xsl:when test="$frequency='b'">
            <field name="accrualPeriodicity">Bimonthly</field>
          </xsl:when>
          <xsl:when test="$frequency='c'">
            <field name="accrualPeriodicity">Semiweekly</field>
          </xsl:when>
          <xsl:when test="$frequency='d'">
            <field name="accrualPeriodicity">Daily</field>
          </xsl:when>
          <xsl:when test="$frequency='e'">
            <field name="accrualPeriodicity">Biweekly</field>
          </xsl:when>
          <xsl:when test="$frequency='f'">
            <field name="accrualPeriodicity">Semiannual</field>
          </xsl:when>
          <xsl:when test="$frequency='g'">
            <field name="accrualPeriodicity">Biennial</field>
          </xsl:when>
          <xsl:when test="$frequency='h'">
            <field name="accrualPeriodicity">Triennial</field>
          </xsl:when>
          <xsl:when test="$frequency='i'">
            <field name="accrualPeriodicity">Three times a week</field>
          </xsl:when>
          <xsl:when test="$frequency='j'">
            <field name="accrualPeriodicity">Three times a month</field>
          </xsl:when>
          <xsl:when test="$frequency='k'">
            <field name="accrualPeriodicity">Continuous</field>
          </xsl:when>
          <xsl:when test="$frequency='m'">
            <field name="accrualPeriodicity">Monthly</field>
          </xsl:when>
          <xsl:when test="$frequency='q'">
            <field name="accrualPeriodicity">Quarterly</field>
          </xsl:when>
          <xsl:when test="$frequency='s'">
            <field name="accrualPeriodicity">Semimonthly</field>
          </xsl:when>
          <xsl:when test="$frequency='t'">
            <field name="accrualPeriodicity">Three times a year</field>
          </xsl:when>
          <xsl:when test="$frequency='w'">
            <field name="accrualPeriodicity">Weekly</field>
          </xsl:when>
        </xsl:choose>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template
    match="marc:datafield[500&lt;= @tag and @tag&lt;= 599 ][not(@tag=506 or @tag=530 or @tag=540 or @tag=546)]">
    <xsl:param name="isHolding"/>
    <xsl:if test="not($isHolding)">
      <field name="description">
        <xsl:value-of select="marc:subfield[@code='a']"/>
      </field>
    </xsl:if>
  </xsl:template>

  <xsl:template match="marc:datafield[@tag=245]">
    <field name="title">
      <xsl:call-template name="chopPunctuation">
        <xsl:with-param name="chopString">
          <xsl:call-template name="subfieldSelect">
            <xsl:with-param name="codes">abfghk</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=100]|marc:datafield[@tag=110]|marc:datafield[@tag=111]">
  	<field name="creatorPrimary"><xsl:call-template name="subfieldSelect" /></field>
  	<field name="creator"><xsl:call-template name="subfieldSelect" /></field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=700]|marc:datafield[@tag=710]|marc:datafield[@tag=711]|marc:datafield[@tag=720]">
    <field name="creator"><xsl:call-template name="subfieldSelect"/></field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=655]">
    <field name="description">
      <xsl:call-template name="subfieldSelect"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=020]">
    <xsl:variable name="isbn">
      <xsl:if test="substring(marc:subfield[@code='a'],11,1)=' '">
        <xsl:value-of select="substring(marc:subfield[@code='a'],1,10)"/>
      </xsl:if>
      <xsl:if test="not(substring(marc:subfield[@code='a'],11,1)=' ')">
        <xsl:value-of select="substring(marc:subfield[@code='a'],1,13)"/>
      </xsl:if>
    </xsl:variable>
    <field name="identifier">
      <xsl:text>URN:ISBN:</xsl:text>
      <xsl:value-of select="$isbn"/>
    </field>
    <field name="isbn">
      <xsl:value-of select="$isbn"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=022]">
    <xsl:if test="marc:subfield[@code='a']">
      <xsl:variable name="issn" select="substring(marc:subfield[@code='a'],1,9)"/>
      <field name="identifier">
        <xsl:text>URN:ISSN:</xsl:text>
        <xsl:value-of select="$issn"/>
      </field>
      <field name="issn">
        <xsl:value-of select="$issn"/>
      </field>
    </xsl:if>
    <xsl:for-each select="marc:subfield[@code='z']">
      <xsl:variable name="issn" select="substring(.,1,9)"/>
      <field name="identifier">
        <xsl:text>URN:ISSN:</xsl:text>
        <xsl:value-of select="$issn"/>
      </field>
      <field name="issn">
        <xsl:value-of select="$issn"/>
      </field>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=035]">
    <field name="identifier">
      <xsl:value-of select="marc:subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=050]">
    <xsl:for-each select="marc:subfield[@code='a']">
      <field name="identifier">
        <xsl:text>URN:LCCN:</xsl:text>
        <xsl:value-of select="."/>
      </field>
      <field name="lccn">
        <xsl:value-of select="."/>
      </field>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=060]">
    <xsl:for-each select="marc:subfield[@code='a']">
      <field name="identifier">
        <xsl:text>URN:nlmCallNumber:</xsl:text>
        <xsl:value-of select="."/>
      </field>
      <field name="nlmCallNumber">
        <xsl:value-of select="."/>
      </field>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=260]">
    <field name="publisher">
      <xsl:call-template name="chopPunctuation">
        <xsl:with-param name="chopString">
          <xsl:call-template name="subfieldSelect">
            <xsl:with-param name="codes">ab</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </field>
    <xsl:if test="marc:subfield[@code='c']">
      <field name="datePublished">
        <xsl:value-of select="marc:subfield[@code='c']"/>
      </field>
    </xsl:if>
  </xsl:template>
  <xsl:template
    match="marc:datafield[@tag=600 or @tag=610 or @tag=611 or @tag=630 or @tag=650 or @tag=653]">
    <xsl:variable name="subject">
      <xsl:call-template name="chopPunctuation">
        <xsl:with-param name="chopString">
          <xsl:call-template name="subfieldSelect"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <field name="subject">
      <xsl:value-of select="$subject"/>
    </field>
    <xsl:choose>
      <xsl:when test="@ind2='0'">
        <field name="subjectLCSH">
          <xsl:value-of select="$subject"/>
        </field>
      </xsl:when>
      <xsl:when test="@ind2='2'">
        <field name="subjectMeSH">
          <xsl:value-of select="$subject"/>
        </field>
      </xsl:when>
    </xsl:choose>
    <xsl:call-template name="optionalField">
      <xsl:with-param name="subfields">y</xsl:with-param>
      <xsl:with-param name="fieldName">temporal</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="optionalField">
      <xsl:with-param name="subfields">z</xsl:with-param>
      <xsl:with-param name="fieldName">geographic</xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="marc:datafield[@tag=648 or @tag=651]">
    <xsl:call-template name="optionalField">
      <xsl:with-param name="subfields">y</xsl:with-param>
      <xsl:with-param name="fieldName">temporal</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="optionalField">
      <xsl:with-param name="subfields">z</xsl:with-param>
      <xsl:with-param name="fieldName">geographic</xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="marc:datafield[@tag=662]">
    <field name="coverage">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">abcdefgh</xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=752]">
    <field name="coverage">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">adcdfgh</xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template
    match="marc:datafield[@tag=760]|marc:datafield[@tag=762]|marc:datafield[@tag=765]|marc:datafield[@tag=767]|marc:datafield[@tag=770]|marc:datafield[@tag=772]|marc:datafield[@tag=773]|marc:datafield[@tag=774]|marc:datafield[@tag=775]|marc:datafield[@tag=776]|marc:datafield[@tag=777]|marc:datafield[@tag=780]|marc:datafield[@tag=785]|marc:datafield[@tag=786]|marc:datafield[@tag=787]">
    <field name="relation">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">ot</xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=856]">
    <field name="url">
      <xsl:value-of select="marc:subfield[@code='u']"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=506 or @tag=540]">
    <field name="rights">
      <xsl:value-of select="marc:subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=130 or @tag=240 or @tag=246 or @tag=730 or @tag=740]">
    <xsl:variable name="alternateTitle">
      <xsl:call-template name="subfieldSelect"/>
    </xsl:variable>
    <xsl:if test="$alternateTitle">
      <field name="alternateTitle">
        <xsl:value-of select="$alternateTitle"/>
      </field>
    </xsl:if>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=250]">
    <field name="edition">
      <xsl:value-of select="marc:subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=300]">
    <field name="physical">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">abc</xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=362]">
    <field name="date">
      <xsl:value-of select="marc:subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=440 or @tag=490]">
    <field name="series">
      <xsl:call-template name="subfieldSelect"/>
    </field>
  </xsl:template>
    <!--
    <xsl:template match="marc:datafield[@tag=050 or @tag=090]/marc:subfield[@code='a']">
    <xsl:if test="string-length()">
    <xsl:call-template name="mapCallnum">
    <xsl:with-param name="callNum"><xsl:value-of select="."/></xsl:with-param>
    </xsl:call-template>
    </xsl:if>
    </xsl:template> -->
  <xsl:template match="marc:datafield[@tag=852]">
    <xsl:variable name="callNumber">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">khim</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    
    <field name="callNumber">
      <xsl:value-of select="$callNumber" />
    </field>
    <xsl:variable name="alternateTitle">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">l</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:if test="$alternateTitle">
      <field name="alternateTitle">
        <xsl:value-of select="$alternateTitle"/>
      </field>
    </xsl:if>
    
    <xsl:choose>
      <xsl:when test="@ind2='0'">
        <field name="identifier">
          <xsl:text>URN:LCCN:</xsl:text>
          <xsl:value-of select="$callNumber" />
        </field>
        <field name="lccn">
          <xsl:value-of select="$callNumber" />
        </field>
      </xsl:when>
      <xsl:when test="@ind2='2'">
        <field name="identifier">
          <xsl:text>URN:nlmCallNumber:</xsl:text>
          <xsl:value-of select="$callNumber" />
        </field>
        <field name="nlmCallNumber">
          <xsl:value-of select="$callNumber" />
        </field>
      </xsl:when>
      <xsl:when test="@ind2='3'">
        <field name="identifier">
          <xsl:text>URN:sdCallNumber:</xsl:text>
          <xsl:value-of select="$callNumber" />
        </field>
        <field name="sdCallNumber">
          <xsl:value-of select="$callNumber" />
        </field>
      </xsl:when>
    </xsl:choose>
    
  </xsl:template>
  <xsl:template match="marc:datafield[@tag=866 or @tag=867 or @tag=868]">
    <xsl:variable name="holdings">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">az</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$holdings">
      <field name="holdings">
        <xsl:value-of select="$holdings"/>
      </field>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="optionalField">
    <xsl:param name="subfields" />
    <xsl:param name="fieldName" />
    
    <xsl:variable name="output">
      <xsl:call-template name="chopPunctuation">
        <xsl:with-param name="chopString">
          <xsl:call-template name="subfieldSelect">
            <xsl:with-param name="codes"><xsl:value-of select="$subfields" /></xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$output">
      <field>
        <xsl:attribute name="name"><xsl:value-of select="$fieldName" /></xsl:attribute>
        <xsl:value-of select="$output" />
      </field>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
